//
//  GradiantShape.m
//  QuartzStudy
//
//  Created by longhuihu on 11-6-22.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GradiantShape.h"
#import "BaseShape.h"

@interface LinearGradient : BaseShape
{
}
@end

@implementation LinearGradient
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGGradientRef myGradient;
	CGColorSpaceRef myColorspace;
	size_t num_locations = 2;
	CGFloat locations[2] = { 0.0, 1.0 };
	CGFloat components[8] = { 1.0, 0.5, 0.4, 1.0,  // Start color
		0.8, 0.8, 0.3, 0.0 }; // End color
	
	myColorspace = CGColorSpaceCreateDeviceRGB();
	myGradient = CGGradientCreateWithColorComponents (myColorspace, components,
													  locations, num_locations);
	
	CGPoint startPoint = self.bounds.origin;
	CGPoint endPoint = CGPointMake(startPoint.x+self.bounds.size.width,startPoint.y+self.bounds.size.height);
	CGContextDrawLinearGradient (context, myGradient, startPoint, endPoint, 0);
	
	[super drawFrame:context];
}
@end


@interface RadialGradient : BaseShape
{
}
@end

@implementation RadialGradient
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGGradientRef myGradient;
	CGColorSpaceRef myColorspace;
	size_t num_locations = 2;
	CGFloat locations[2] = { 0.0, 1.0 };
	CGFloat components[8] = { 1.0, 0.5, 0.4, 1.0,  // Start color
		0.8, 0.8, 0.3, 1.0 }; // End color
	
	myColorspace = CGColorSpaceCreateDeviceRGB();
	myGradient = CGGradientCreateWithColorComponents (myColorspace, components,
													  locations, num_locations);
	
	CGPoint startPoint = CGPointMake(30,30);
	CGPoint endPoint = CGPointMake(60,60);
	CGContextDrawRadialGradient(context, myGradient, startPoint, 20,endPoint, 30,0);
	
	[super drawFrame:context];
}
@end


@implementation GradiantShape

- (id)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
		LinearGradient *linear = [[LinearGradient alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
		[self addSubview:linear];
		[linear release];
		
		RadialGradient *radial = [[RadialGradient alloc] initWithFrame:CGRectMake(100, 0, 100, 100)];
		[self addSubview:radial];
		[radial release];
	}
	return self;
}

@end
